package com.yupi.yuaiagent.tools;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import com.yupi.yuaiagent.constant.FileConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

import java.io.File;

/**
 * 文件操作工具类
 */
public class FileOperationTool {

    private final String File_DIR = FileConstant.FILE_SAVE_DIR + File.separator + "file";

    @Tool(description = "Read content form a file")
    public String readFile(@ToolParam(description = "Name of the file to read") String fileName) {
        String filePath = File_DIR + "/" + fileName;
        try {
            return FileUtil.readUtf8String(filePath);
        } catch (IORuntimeException e) {
            return "Error reading file:" + fileName + ": " + e.getMessage();
        }
    }

    @Tool(description = "Write conten to a file")
    public String writeFile(
            @ToolParam(description = "Name of the file to write") String fileName,
            @ToolParam(description = "Content to write") String content) {
        String filePath = File_DIR + "/" + fileName;
        try {
            FileUtil.mkdir(File_DIR);
//            FileUtil.touch(filePath);
            FileUtil.writeUtf8String(content, filePath);
            return "File written successfully: " + fileName;
        } catch (Exception e) {
            return "Error writing file: " + fileName + ": " + e.getMessage();
        }
    }

}
